package com.konsung.presenter.impl;

import com.konsung.bean.PatientBean;
import com.konsung.greendaoUtil.GreenDaoManager;
import com.konsung.greendaoUtil.PatientBeanDao;
import com.konsung.kslibrary.base.BasePresenter;
import com.konsung.presenter.AlterPatientPresenter;
import com.konsung.ui.AlterPatientActivity;
import com.konsung.util.BroadcastUtils;
import com.konsung.util.ReferenceUtils;
import com.konsung.util.SpUtils;
import com.konsung.util.constant.GlobalConstant;

import java.util.List;

/**
 * 逻辑实现类
 */

public class AlterPatientPresenterImpl extends BasePresenter<AlterPatientActivity> implements
        AlterPatientPresenter.Presenter {

    @Override
    public PatientBean getCurrentPatient() {
        String sp = SpUtils.getSp(mvpView, GlobalConstant.APP_CONFIG, GlobalConstant.CURRENT_PATIENT
                , "");
        List<PatientBean> list = GreenDaoManager.getPatientDao(mvpView).queryBuilder()
                .where(PatientBeanDao.Properties.Idcard.eq(sp)).list();
        return list.get(0);
    }

    @Override
    public void alterPatient(PatientBean bean) {
        if (bean != null) {
            GreenDaoManager.getPatientDao(mvpView).update(bean);
            mvpView.alterResult(AlterPatientActivity.AlterState.SUCCESS);
            ReferenceUtils.initPersonalReference(); //修改当前居民信息，更新参考值范围
            BroadcastUtils.sendReplacePatientForAll(); // 当前居民信息发生变化，通知其他app更新
        }
    }
}
